package com.fzhucc.modules.businessCard.rest;

import com.fzhucc.annotation.AnonymousAccess;
import com.fzhucc.annotation.Log;
import com.fzhucc.modules.businessCard.domain.BusinessCardPre;
import com.fzhucc.modules.businessCard.domain.dto.BusinessCardPreQueryCriteria;
import com.fzhucc.modules.businessCard.service.BusinessCardPreService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
* @author yyg
* @date 2022-12-21
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "名片：预备名片管理")
@RequestMapping("/api/businessCardPre")
public class BusinessCardPreController {

    private final BusinessCardPreService businessCardPreService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('businessCardPre:list')")
    public void exportBusinessCardPre(HttpServletResponse response, BusinessCardPreQueryCriteria criteria) throws IOException {
        businessCardPreService.download(businessCardPreService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询businessCardPre")
    @ApiOperation("查询businessCardPre")
    @PreAuthorize("@el.check('businessCardPre:list')")
    public ResponseEntity<Object> queryBusinessCardPre(BusinessCardPreQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(businessCardPreService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增businessCardPre")
    @ApiOperation("新增businessCardPre")
    @PreAuthorize("@el.check('businessCardPre:add')")
    public ResponseEntity<Object> createBusinessCardPre(@Validated @RequestBody BusinessCardPre resources){
        return new ResponseEntity<>(businessCardPreService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改businessCardPre")
    @ApiOperation("修改businessCardPre")
    @PreAuthorize("@el.check('businessCardPre:edit')")
    public ResponseEntity<Object> updateBusinessCardPre(@Validated @RequestBody BusinessCardPre resources){
        businessCardPreService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除businessCardPre")
    @ApiOperation("删除businessCardPre")
    @PreAuthorize("@el.check('businessCardPre:del')")
    public ResponseEntity<Object> deleteBusinessCardPre(@RequestBody Integer[] ids) {
        businessCardPreService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}